package com.apes.auto.autoparts.model;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.fileserver.File;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import com.apes.scm.masterdata.product.model.Category;
import com.apes.scm.masterdata.product.repository.CategoryRepository;
import com.apestech.framework.util.StringUtil;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;


/***
 * 针对某一行确认数据进行编译
 */

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = EpcPartCodeParse.CONST_TABLE_NAME)
public class EpcPartCodeParse extends Model {
    public static final String CONST_TABLE_NAME = "epc_part_code_parse";

    @Id
    @Comment("编码")
    @Column(name = "id", nullable = false, length = 120)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "EPC零件编译（线上）", padding = 8)
    private String id;

    @Comment("车架号")
    @Column(name = "vin")
    private String vin;

    @Comment("配置车型编码")
    @Column(name = "pzcxid")
    private String pzcxid;

    @Comment("汽车厂牌名称")
    @Column(name = "qccpmc")
    private String qccpmc;

    @Comment("EPC版本")
    @Column(name = "epc_version")
    private String epcVersion;

    @Comment("兆配云子组目录名称")
    @Column(name = "menu_name")
    private String menuName;

    @Comment("零件编码")
    @Column(name = "part_code")
    private String partCode;

    @Comment("零件名称")
    @Column(name = "part_name")
    private String partName;

    @Column(name = "image_sn")
    @Comment("图号")
    private String imageSN;

    @File
    @Lob
    @Comment("图片")
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "image_url", columnDefinition = "CLOB")
    private String imageUrl;

    @Comment("门店")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    private Dept dept;

    @Column(name = "parse_flag", nullable = false, columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("处理标记")
    private boolean parseFlag = false;

    @Column(name = "error_flag", nullable = false, columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("错误标记")
    private boolean errorFlag = false;

    @Column(name = "remark", length = 4000)
    @Comment("备注")
    private String remark;

    @Comment("配件名称")
    @Column(name = "part_description", length = 4000)
    private String partDescription;

    public void setPartDescription(String partDescription) {
    }

    @Transient
    private List<Category> categories;

    public List<Category> getCategories() {
        if (isWrite() || isLoad()) {
            categories = convertToList(this.partDescription);
        }
        return categories;
    }

    public void setCategories(List<Category> categories) {
        this.categories = categories;
        if (!isWrite()) {
            this.partDescription = convertToString(this.categories);
        }
    }


    private List<Category> convertToList(String source) {
        if (StringUtil.isEmpty(source)) return null;
        List<Category> categories = new ArrayList<>();
        String[] rows = source.split(";");
        for (String row : rows) {
            if (row.contains(":")) {
                categories.add(SpringManager.getBean(CategoryRepository.class).findOne(String.valueOf(StringUtils.split(row, ":")[0])));
            }
        }
        return categories;
    }


    private String convertToString(List<Category> categories) {
        StringBuffer sb = new StringBuffer();
        for (Category category : categories) {
            if (sb.length() > 0) {
                sb.append(";");
            }
            sb.append(category.getId());
            sb.append(":");
            sb.append(category.getName());  // 格式 id:name;
        }
        return sb.toString();
    }



    @Transient
    private PartyRole _creator;

    @Transient
    public PartyRole get_creator() {
        if(this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }


    @Transient
    private PartyRole _updater;

    @Transient
    public PartyRole get_updater() {
        if(this.getUpdater() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getUpdater());
    }
}
